import ajaxOptions = require("AjaxCallsOptions");
import config = require("Configuration");
import dataAccess = require("Data-access");
import application = require("WindowExtensions");

export class MovieList extends kendo.data.ObservableObject {

    movieList: any;

    constructor() {
        super();
        super.init(this);
    }

    private _instance = this;

    getMovieList = (listType: number) => {
        var movieListOptions = new ajaxOptions.AjaxOptions();
        movieListOptions.url = config.Configuration.getMovieListUrl;
        movieListOptions.requestType = "GET";
        movieListOptions.dataType = "JSON";
        movieListOptions.data = { listType: listType };

        dataAccess.callService(movieListOptions, this._instance, this.fnGetMoviesCallBack);
    };

    fnGetMoviesCallBack(model: kendo.data.ObservableObject, result: ajaxOptions.AjaxOptionsCallbackInput) {
        if (result.success !== true) {
            alert("Error generated in fnGetMoviesCallBack");
        } else {
            model.set("movieList", result.data);
        }
    }

    listTypeSelected(e) {
        this.getMovieList(e.sender.selectedIndex);
    }

    init() {
        this.getMovieList(0);
    }
}

var appInit = application.getAppInitProperty();
appInit.movieListInit = () => {
    var viewModel = new MovieList();
    viewModel.init();
    kendo.bind($("#mt-home-main-view"), viewModel);
}